package org.springblade.modules.system.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.AllArgsConstructor;
import org.flowable.idm.engine.impl.persistence.entity.UserEntity;
import org.springblade.core.log.model.LogApi;
import org.springblade.core.log.model.LogError;
import org.springblade.core.log.model.LogUsual;
import org.springblade.core.secure.BladeUser;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.modules.log.entity.LogService;
import org.springblade.modules.log.service.IServiceLogService;
import org.springblade.modules.system.entity.User;
import org.springblade.modules.system.service.*;
import org.springframework.stereotype.Service;


@Service
@AllArgsConstructor
public class LogServiceImpl implements ILogService {

	private final ILogUsualService usualService;
	private final ILogApiService apiService;
	private final ILogErrorService errorService;
	private final IServiceLogService serviceLogService;
	private final IUserService userService;

	@Override
	public Boolean saveUsualLog(LogUsual log) {
		User user = userService.getOne(Wrappers.<User>query().lambda().eq(User::getAccount, log.getCreateBy()));
		if (null!=user)log.setCreateBy(user.getRealName());
		log.setServiceId("training-api");
		return usualService.save(log);
	}

	@Override
	public Boolean saveApiLog(LogApi log) {
		User user = userService.getOne(Wrappers.<User>query().lambda().eq(User::getAccount, log.getCreateBy()));
		if (null!=user)log.setCreateBy(user.getRealName());
		log.setServiceId("training-api");
		return apiService.save(log);
	}

	@Override
	public Boolean saveErrorLog(LogError log) {
		User user = userService.getOne(Wrappers.<User>query().lambda().eq(User::getAccount, log.getCreateBy()));
		if (null!=user)log.setCreateBy(user.getRealName());
		log.setServiceId("training-api");
		return errorService.save(log);
	}

	@Override
	public Boolean saveServiceLog(LogService log) {
		log.setServiceId("training-api");
		return serviceLogService.save(log);
	}

}
